//1. 安装 mongoose
//2. 导入
const mongoose = require('mongoose');

//3. 连接 mongodb 服务器
mongoose.connect('mongodb://127.0.0.1:27017/project');

//4. 设置回调
mongoose.connection.on('open', () => {
    //5. 创建文档结构对象
    const BookSchema = new mongoose.Schema({
        name: String,
        author: String,
        price: Number
    });

    //6. 创建模型对象                集合名称: 创建集合时, 会自动将名字改为『复数』
    const BookModel = mongoose.model('book', BookSchema);

    //7. 更新数据  update 更新  one 单个    默认就是『局部更新』
    // BookModel.updateOne({name: '米小圈'}, {price: 9.9}, (err,data) => {
    //     if(err) throw err;
    //     console.log(data);
    //     //8. 选做
    //     mongoose.connection.close();
    // });

    //批量更新
    BookModel.updateMany({
        price: {$gt: 20}
    }, {price: 19.9}, (err, data) => {
        if(err) throw err;
        console.log(data);
        mongoose.connection.close();
    })

})

mongoose.connection.on('error', () => {});